package com.ctriposs.baiji.rpc.samples.movie;

import com.ctriposs.baiji.rpc.common.BaijiContract;
import com.ctriposs.baiji.rpc.common.ctrip.config.ConfigStore;
import com.ctriposs.baiji.rpc.server.BaijiServiceHost;
import com.ctriposs.baiji.rpc.server.HostConfig;
import com.ctriposs.baiji.rpc.server.ServiceHost;
import com.ctriposs.baiji.rpc.server.netty.NonBlockingHttpServerBuilder;
import com.ctriposs.baiji.rpc.server.registry.ConfigStoreBasedRegistry;
import com.ctriposs.baiji.rpc.server.registry.ServiceInfo;
import com.ctriposs.baiji.rpc.server.registry.ServiceRegistry;
import io.netty.channel.ChannelOption;

public final class StartNonBlockingServer {

    public static void main(String[] args) throws Exception {
        HostConfig config = new HostConfig();
        config.debugMode = true;
        ServiceHost host = new BaijiServiceHost(config, MovieServiceImpl.class);

        NonBlockingHttpServerBuilder builder = new NonBlockingHttpServerBuilder(8080);

        builder.serviceHost(host)
                .withSelectorCount(1)
                .executorThreadCount(10)
                .serverSocketOption(ChannelOption.SO_BACKLOG, 100)
                .clientSocketOption(ChannelOption.TCP_NODELAY, true)
                .build().startWithoutWaitingForShutdown();

        ConfigStore.initialize("http://ws.config.framework.fws.qa.nt.ctripcorp.com/Configws/", "921889");
        ServiceRegistry registry = new ConfigStoreBasedRegistry(ConfigStore.getInstance());
        BaijiContract contract = MovieService.class.getAnnotation(BaijiContract.class);
        ServiceInfo service = new ServiceInfo.Builder().serviceName(contract.serviceName())
                .serviceNamespace(contract.serviceNamespace())
                .subEnv("dev")
                .port(8080).build();
        registry.addService(service);
        registry.run();
    }
}
